import 'package:flutter/material.dart';
import './PPHotSaleCommodityItem.dart';

class PPHorizontalScrollView extends StatefulWidget {

    PPHorizontalScrollView(List<Map> data, double itemWidth, double itemHeight, { Key key, this.onClick }): this.data = data, this.itemWidth = itemWidth, this.itemHeight = itemHeight, super(key: key);

    final List<Map> data;
    final double itemWidth;
    final double itemHeight;
    final Function onClick;

    _PPHorizontalScrollViewState createState() => _PPHorizontalScrollViewState();
}

class _PPHorizontalScrollViewState extends State<PPHorizontalScrollView> {

    List<Map> data;
    double itemWidth;
    double itemHeight;

    @override
    void initState() {
        data = widget.data;
        itemWidth = widget.itemWidth;
        itemHeight = widget.itemHeight;
        super.initState();
    }

    @override
    void didUpdateWidget(PPHorizontalScrollView oldWidget) {
        data = widget.data;
        itemWidth = widget.itemWidth;
        itemHeight = widget.itemHeight;
        super.didUpdateWidget(oldWidget);
    }

    @override 
    Widget build(BuildContext context) {
        return (Container(
            color: Colors.white,
            child: SingleChildScrollView(
                padding: EdgeInsets.fromLTRB(10, 15, 10, 15),
                controller: new ScrollController(),
                scrollDirection: Axis.horizontal,
                primary: false,
                child: Row(
                    children:_getScrollViewItems(),
                )
            )
        ));
    }

    _getScrollViewItems() {
        return data.map((item){
            int index = data.indexOf(item);
            return (PPHotSaleCommodityItem(item, itemWidth, itemHeight, index: index, onPressAction: _onPressScrollViewItem));
        }).toList();
    }

    _onPressScrollViewItem(Map item, int index) {
        if(widget.onClick != null) {
            widget.onClick(item, index);
        }
    }
}